﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AntdUI_Halcon.Pages
{
    public partial class HalconControl : UserControl
    {
        private HImage ho_Image;
        //绘制ROI
        HDrawingObject HDrawingObject = new HDrawingObject() ;

        public HalconControl()
        {
            InitializeComponent();


        }

        private void button1_Click(object sender, EventArgs e)
        {

            // 配置文件对话框,添加图像文件
            openFileDialog1.Title = "选择图像文件";
            openFileDialog1.Filter = "所有支持的图像|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff|" +
                                     "JPEG 图像|*.jpg;*.jpeg|" +
                                     "PNG 图像|*.png|" +
                                     "BMP 图像|*.bmp|" +
                                     "GIF 图像|*.gif|" +
                                     "TIFF 图像|*.tiff|" +
                                     "所有文件|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = false;

            // 确认添加成功
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的路径
                var imgUrl = openFileDialog1.FileName;
                //转换为Halcon图像
                ho_Image = new HImage(imgUrl);

                //获取窗口控件的宽度和高度
                int windowWidth = HWindowControl.Width;
                int windowHeight = HWindowControl.Height;

                // 获取图像的原始宽度和高度
                HTuple widthTuple, heightTuple;
                HOperatorSet.GetImageSize(ho_Image, out widthTuple, out heightTuple);
                int Width = widthTuple.I;
                int Height = heightTuple.I;

                //计算图像和窗口的宽高比
                double windowAspectRatio = (double)windowWidth / windowHeight;
                double imageAspectRatio = (double)Width / Height;

                // 根据宽高比计算显示区域，确保图像比例不变
                int partWidth, partHeight;
                if (imageAspectRatio > windowAspectRatio)
                {
                    //图像更宽，限制宽度，调整高度
                    partWidth = Width;
                    partHeight = (int)(Width / windowAspectRatio);
                }
                else
                {
                    //图像更高，限制高度，调整宽度
                    partHeight = Height;
                    partWidth = (int)(Height * windowAspectRatio);
                }

                //设置Halcon窗口的显示区域，使其包含完整图像的居中部分
                int partOffsetX = (partWidth - Width) / 2;
                int partOffsetY = (partHeight - Height) / 2;
                HOperatorSet.SetPart(HWindowControl.HalconWindow, -partOffsetY, -partOffsetX, partHeight - partOffsetY - 1, partWidth - partOffsetX - 1);

                // 显示图像
                HOperatorSet.DispObj(ho_Image, HWindowControl.HalconWindow);

            }
        }

        private void btnROI_Click(object sender, EventArgs e)
        {
            //设置ROI框大小
             HDrawingObject.CreateDrawingObjectRectangle1(100, 100, 400, 500);
            //显示在Halcon窗口
            //HWindowControl.HalconWindow.AttachDrawingObjectToWindow(HDrawingObject);

            // 1. 保存当前显示的图像
            HImage currentImage = HWindowControl.HalconWindow.DumpWindowImage();

            // 2. 执行操作，比如附加绘图对象到窗口
            HWindowControl.HalconWindow.AttachDrawingObjectToWindow(HDrawingObject);

            // 3. 恢复原始图像并在其上显示绘图对象
            HWindowControl.HalconWindow.DispObj(currentImage);

        }
    }
}
